Tip Corner - Reading DATA into Arrays

Level: Beginner

by Alyce Watson [http://alycesrestaurant.com/]

Home

API Translator

Window Help

Communication

Slider Control

Desktop Shortcuts

Quick Visual Designer

Rules for Code

DATA and Arrays

Easy Polygon

Triangle Draw

Control a Sprite

Shell to DOS

Morse Code

Batch Files

Multicolumn Listbox

Newsletter help

Index


DATA

DATA statements can contain strings or numbers. Strings should be enclosed in quotation marks, although that isn't strictly necessary if string data items are single words. Here are three sample DATA statements. The first one contains string elements, the second one contains numbers, and the third contains a mix of the two.

DATA "United Kingdom","Belgium","Austria","Japan","Kenya"
DATA 15,233,67,8889,1,3.2
DATA "one",1,"two",2,"three",3,"four",4

All DATA statements in a program belong to a single block of DATA, no matter where they are located. One DATA statement can be on line one and the next on line 1000, but they still create a single block of DATA.

READ

The READ command reads the next DATA item into a variable. String items should be read into string variables and numeric items should be read into numeric variables. If there are no more DATA items to READ, the program halts with an error: "Read past end of DATA." You can eliminate the possibility of error by counting the number of DATA items carefully and ensuring that the program doesn't attempt to read more DATA than is contained in the program. You can also put an ending tag of some sort on the last DATA item. When the tag is READ, the program knows to stop READING DATA. This sample uses the word "final" to tag the end of the DATA statements. Notice that the DATA is contained at the start of the code. The program skips over it and executes the WHILE/WEND.

DATA "salt","pepper","butter","sugar","final"
var$=""
WHILE var$ <> "final"
    READ var$
    PRINT var$
WEND

RESTORE

DATA items are read from the start of the DATA block to the end of the items. DATA statements can be placed after labels in code so that specified parts of the DATA block can be accessed. The RESTORE command causes the next DATA item after the specified branch label to be READ next. Here is a demo that READS a single item from each block of DATA, then uses RESTORE to move to a different DATA block for the next READ operation. Notice that it goes back to the beginning of the DATA to start over.

READ var$
print var$

RESTORE [numerals]
READ var
print var

RESTORE [numbers]
READ var$
print var$

RESTORE [places] 'start over
READ var$
print var$

END

[places]
DATA "United Kingdom","Belgium","Austria","Japan","Kenya"
[numerals]
DATA 15,233,67,8889,1,3.2
[numbers]
DATA "one",1,"two",2,"three",3,"four",4

If no branch label is specified, RESTORE starts again at the beginning of the DATA block. Here is another way to gurantee that the program doesn't halt with an error when READING DATA. It checks for the ending tag (in this case it is the word "final") and uses the RESTORE command when the end of the DATA block is encountered.

DATA "salt","pepper","butter","sugar","final"

FOR i = 1 to 20
    READ var$
    IF var$<> "final" THEN
        PRINT var$
    ELSE
        RESTORE
    END IF
NEXT

Arrays

DATA statements provide a quick and easy way to fill arrays. If you want to fill an array with the 16 names of Liberty BASIC colors, it looks like this:

colors$(1) = red
colors$(2) = darkred
colors$(3) = green
colors$(4) = darkgreen
colors$(5) = blue
colors$(6) = darkblue
colors$(7) = cyan
colors$(8) = darkcyan
colors$(9) = pink
colors$(10) = darkpink
colors$(11) = yellow
colors$(12) = brown
colors$(13) = white
colors$(14) = black
colors$(15) = lightgray
colors$(16) = darkgray

Loops

It is much easier to list the color names in DATA statements, then READ them into the array in a loop. You cannot READ DATA directly into an array. You must first READ it into a variable, then fill the array element with the value of the variable:

READ var$
array$(1)=var$

Arrays are filled with items from DATA statements in FOR/NEXT loops. It takes much less typing to fill an array in this manner. The larger the array, the more typing that is eliminated.

DIM colors$(16)

FOR i = 1 to 16
    READ col$
    colors$(i) = col$
NEXT

DATA "red","darkred","green","darkgreen","blue","darkblue"
DATA "cyan","darkcyan","pink","darkpink","yellow","brown"
DATA "white","black","lightgray","darkgray"

DEMO

Here is a well-commented demo that places the Liberty BASIC color names into DATA statements, then uses them to fill an array. The array is printed to the mainwindow. It is later sorted, then printed again.


'DIM array
DIM colors$(16)

'Fill array from DATA statements in loop.
FOR i = 1 to 16
    'READ next DATA item into col$ variable
    READ col$
    'fill next array element with col$ variable
    colors$(i) = col$
NEXT

'print array elements in loop.
PRINT "Unsorted array:"
FOR i = 1 to 16
    PRINT "Color ";i;" is ";colors$(i)
NEXT

'sort array
SORT colors$(),0,16

'Print elements of sorted array in loop.
PRINT:PRINT "Sorted array:"
FOR i = 1 to 16
    PRINT "Color ";i;" is ";colors$(i)
NEXT

'stop program
END

'DATA statements:
DATA "red","darkred","green","darkgreen","blue","darkblue"
DATA "cyan","darkcyan","pink","darkpink","yellow","brown"
DATA "white","black","lightgray","darkgray"


Home

API Translator

Window Help

Communication

Slider Control

Desktop Shortcuts

Quick Visual Designer

Rules for Code

DATA and Arrays

Easy Polygon

Triangle Draw

Control a Sprite

Shell to DOS

Morse Code

Batch Files

Multicolumn Listbox

Newsletter help

Index